//
//  ZCBaseViewModel.m
//  ZCJZ
//
//  Created by sujeking on 2019/3/12.
//  Copyright © 2019年 jeking. All rights reserved.
//

#import "ZCBaseViewModel.h"
#import "SKBaseWebService.h"
#import "AppAndDeviceTool.h"
#import "ZCBaseUserManager.h"
#import <MBProgressHUD.h>
@implementation ZCBaseViewModel

+ (void)uploadFile2ServerWithImg:(UIImage *)img
                        fileName:(NSString *)fileName
                           owner:(UIViewController *)vc
                        complete:(void (^)(ZCErrorCode code, NSString *msg,id info))block {
    NSData *data = UIImageJPEGRepresentation(img,0.5);
    [SKBaseWebService uploadRequest:@"/v1/index/upload-files"
                         parameters:nil
                      formDataArray:@[data,@"file",fileName,@"*/*"]
                           progress:^(NSProgress *progress) {
                               NSLog(@"%@",progress);
                           }
                            success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    [MBProgressHUD hideHUDForView:vc.view animated:YES];
                                });
                                block(status,msg,data);
                            } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    [MBProgressHUD hideHUDForView:vc.view animated:YES];
                                });
                                block(status,msg,nil);
                            }];
}

+ (void)uploadFile2ServerWithVideo:(id)video
                          fileName:(NSString *)fileName
                             owner:(UIViewController *)vc
                          complete:(void (^)(ZCErrorCode code, NSString *msg,id info))block {
    NSData *data = video;
    [SKBaseWebService uploadRequest:@"/v1/index/upload-files"
                         parameters:nil
                      formDataArray:@[data,@"file",fileName,@"*/*"]
                           progress:^(NSProgress *progress) {
                               NSLog(@"%@",progress);
                           }
                            success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    [MBProgressHUD hideHUDForView:vc.view animated:YES];
                                });
                                block(status,msg,data);
                            } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    [MBProgressHUD hideHUDForView:vc.view animated:YES];
                                });
                                block(status,msg,nil);
                            }];
}

+ (void)collectionActionWithKey:(NSString *)KeyStr
                         action:(NSString *)action
                           data:(id)data
                       complete:(void (^)(ZCErrorCode code, NSString *msg,id info))block {
    
    ZCBaseUserManager *manager = [ZCBaseUserManager shareInstance];
    ZCBaseUserModel *userModel = manager.userModel;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    NSInteger timeint = ceil([[NSDate date] timeIntervalSince1970] * 1000);
    NSString *timer_z = @(timeint).stringValue;

    
    NSMutableDictionary *mdata = [NSMutableDictionary new];
    [mdata setValue:KeyStr forKey:@"key"];
    [mdata setValue:action forKey:@"action"];
    [mdata setValue:data forKey:@"data"];
    [mdata setValue:timer_z forKey:@"trigger_time"];
    [mdata setValue:@"ios" forKey:@"source"];
    [mdata setValue:userModel.member.idstr forKey:@"uid"];
    
    
    AppAndDeviceTool *tool = [AppAndDeviceTool shareInstance];
    [parameters setValue:tool.appVersionStr forKey:@"appVersion"];
    [parameters setValue:tool.appNameStr forKey:@"appName"];
    [parameters setValue:@"ios" forKey:@"systemType"];
    [parameters setValue:tool.deviceNameStr forKey:@"deviceName"];
    [parameters setValue:tool.connectionTypeStr forKey:@"connectionType"];
    [parameters setValue:tool.deviceIdStr forKey:@"deviceId"];
    [parameters setValue:tool.deviceModelStr forKey:@"deviceModel"];
    [parameters setValue:tool.deviceSystemVersionStr forKey:@"systemVersion"];
    [parameters setValue:@"iPhone" forKey:@"uiModel"];
    [parameters setValue:tool.channelStr forKey:@"channel"];
    [parameters setValue:tool.operatorStr forKey:@"operator"];
    [parameters setValue:tool.deviceTokenStr forKey:@"deviceToken"];
    [parameters setValue:@[mdata] forKey:@"data"];

#if DEBUG
    /// 测试
    NSString * MainURLStr = @"https://wzgteststatapi.ec-cloudtech.com";
#else
    /// 域名01【】
    NSString * MainURLStr = @"https://wzgstatapi.ec-cloudtech.com";
#endif
    [SKBaseWebService thirdRequest:@"/v1/data/collection" urls:MainURLStr post:YES parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
        
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}


@end
